import { ArticlesListType } from './search';

const initialState = {
  HomeList: {} as HomeListType,
  ArtcleDetail: {} as ArtcleDetailType,
};

export default function ArticleReducer(
  state = initialState,
  action: ArtclesTypeAction
): typeof initialState {
  switch (action.type) {
    /** 复用存储文章列表 */
    case 'articles/SaveHomeList':
      const { channel_id, pre_timestamp, results } = action.payload;
      return {
        ...state,
        HomeList: {
          ...state.HomeList,
          [channel_id]: {
            pre_timestamp,
            results: state.HomeList[channel_id]
              ? [...state.HomeList[channel_id].results, ...results]
              : [...results],
          },
        },
      };

    /** 根据id覆盖results文章内容 */
    case 'articles/CoverListById':
      const coverId = action.payload.channel_id;
      return {
        ...state,
        HomeList: {
          ...state.HomeList,
          [coverId]: { ...action.payload },
        },
      };

    /** 根据ID保留文章详情内容 */
    case 'articles/SetArtcleDetail':
      return {
        ...state,
        ArtcleDetail: action.payload,
      };
    /** 清空文章详情 */
    case 'articles/ClearDetail':
      return {
        ...state,
        ArtcleDetail: {} as ArtcleDetailType,
      };
    /** 控制是否点赞*/
    case 'articles/Setisattitude':
      return {
        ...state,
        ArtcleDetail: {
          ...state.ArtcleDetail,
          attitude: action.payload,
        },
      };
    /** 控制是否收藏*/
    case 'articles/Setis_collected':
      return {
        ...state,
        ArtcleDetail: {
          ...state.ArtcleDetail,
          is_collected: action.payload,
        },
      };
    /** 控制用户关注*/
    case 'articles/Setis_followed':
      return {
        ...state,
        ArtcleDetail: {
          ...state.ArtcleDetail,
          is_followed: action.payload,
        },
      };
    default:
      return state;
  }
}

export type ArtclesTypeAction =
  | {
      type: 'articles/SaveHomeList';
      payload: SaveHomeListPayloadType;
    }
  | {
      type: 'articles/CoverListById';
      payload: SaveHomeListPayloadType;
    }
  | {
      type: 'articles/SetArtcleDetail';
      payload: ArtcleDetailType;
    }
  | {
      type: 'articles/ClearDetail';
    }
  | {
      type: 'articles/Setisattitude';
      payload: number;
    }
  | {
      type: 'articles/Setis_collected';
      payload: boolean;
    }
  | {
      type: 'articles/Setis_followed';
      payload: boolean;
    };

export type HomeListType = {
  [channel_id: number]: {
    results: ArticlesListType[];
    pre_timestamp: string;
  };
};

type SaveHomeListPayloadType = {
  channel_id: number;
  results: ArticlesListType[];
  pre_timestamp: string;
};

export type ArtcleDetailType = {
  art_id: string;
  title: string;
  pubdate: string;
  aut_id: string;
  content: string;
  aut_name: string;
  aut_photo: string;
  is_followed: boolean;
  is_collected: boolean;
  attitude: number;
  comm_count: number;
  read_count: number;
  like_count: number;
};
